package com.remist.web.action;

import java.util.List;

import com.remist.model.ImageMapBean;
import com.remist.model.Product;
import com.remist.service.ImagesService;
import com.remist.service.ProductService;
import com.remist.service.ProductTypeService;

public class ProductAction   extends BaseAction {
	/**	serialVersionUID */
	private static final long serialVersionUID = -4357112223517529312L;
	private ProductService productService;
	private ImagesService imagesService;
	private ProductTypeService producteTypeService;
	private Product productBean;
	private List<Product> products;
	private String[] imagesIds;
	private List<ImageMapBean> images;
	public List<ImageMapBean> getImages() {
		return images;
	}
	public void setImages(List<ImageMapBean> images) {
		this.images = images;
	}
	public String[] getImagesIds() {
		return imagesIds;
	}
	public void setImagesService(ImagesService imagesService) {
		this.imagesService = imagesService;
	}
	public void setImagesIds(String[] imagesIds) {
		this.imagesIds = imagesIds;
	}
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	public Product getProductBean() {
		return productBean;
	}
	public void setProductBean(Product productBean) {
		this.productBean = productBean;
	}
	public ProductTypeService getProducteTypeService() {
		return producteTypeService;
	}
	public void setProducteTypeService(ProductTypeService producteTypeService) {
		this.producteTypeService = producteTypeService;
	}
	public ProductService getProductService() {
		return productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	public String listProduct() throws Exception {
		products = productService.getAllProduct(0, " 1=1 ");
		return SUCCESS;
	}
 
	public String productAdd() throws Exception {
	 
		productService.addProduct(productBean);
		if ( imagesIds != null )
		for(int i=0;i<imagesIds.length;i++){
			ImageMapBean image = new ImageMapBean();
			image.setReferenceId( productBean.getId() );
			image.setId( Integer.parseInt( imagesIds[i] ));
			imagesService.updateImageRelativeProduct(image);
		}
//		productBean = new Product();
		return SUCCESS;
	}
	
	public String productBeforeUpdate() throws Exception {
		productBean = productService.getProductById(productBean);
		ImageMapBean image = new ImageMapBean();
		image.setReferenceId(productBean.getId());
		images  = imagesService.getImagesByReferenceId(image );
		return SUCCESS;
	}
	
	public String productUpdate() throws Exception {
		if ( imagesIds != null )
			for(int i=0;i<imagesIds.length;i++){
				ImageMapBean image = new ImageMapBean();
				image.setReferenceId( productBean.getId() );
				image.setId( Integer.parseInt(imagesIds[i]) );
				imagesService.updateImageRelativeProduct(image);
//					System.out.println( imagesIds[i] );
			}
		productService.updateProduct(productBean);
		return SUCCESS;
	}
	
	public String delProduct() throws Exception {
		productService.deleteProduct(productBean);
		return SUCCESS;
	}
}
